#!/usr/bin/env python

import sys
from PyQt4 import QtCore, QtGui
from PyQt4.QtCore import *
from PyQt4.QtGui import *

from DigitalClock import *
from charactermap import *
import os

sys.path.append(os.getcwd()+'/findbug')
sys.path.append(os.getcwd()+'/simulatetouch')
sys.path.append(os.getcwd()+'/emmc')
sys.path.append(os.getcwd()+'/property')
sys.path.append(os.getcwd()+'/kmemleak')
sys.path.append(os.getcwd()+'/bootchart')



from FindbugForm import *
from SimulateTouch import *
from SelectProcessForm import *
from  EMMCForm import *
from MonkeyTestForm import *
from PropertyForm import *
from BootchartForm import *
from KmemleakForm import *

#class MyForm(QDialog):
class MyForm(QtGui.QMainWindow):

    cpuPics=[
            'cpuroundmeter.png',
            'cpusimpleline.png',
            'memroundmeter.png',
            'cpumultiline.png',
            'memsimplearea.png',
             ]

    NumButtons = 4
    """
    def createHorizontalGroupBox(self):
        self.horizontalGroupBox = QtGui.QGroupBox("Horizontal layout")
        layout = QtGui.QHBoxLayout()

        for i in range(MyForm.NumButtons):
            button = QtGui.QPushButton("Button %d" % (i + 1))
            layout.addWidget(button)

        self.horizontalGroupBox.setLayout(layout)
    """

    def  showCpu(self):
        self.Cpu = QtGui.QGroupBox("cpu status")
        layout = QtGui.QHBoxLayout()
        self.cpuimageLabel = QtGui.QLabel('')
        layout.addWidget(self.cpuimageLabel)
        self.Cpu.setLayout(layout)

    def  showMem(self):
        self.Mem = QtGui.QGroupBox("memory status")
        layout = QtGui.QHBoxLayout()
        self.memLabel = QtGui.QLabel('')
        layout.addWidget(self.memLabel)
         
        self.Mem.setLayout(layout)

    def  showMem_more(self):
        self.Mem_more = QtGui.QGroupBox("more memory status")
        layout = QtGui.QHBoxLayout()
        self.memmeterLabel = QtGui.QLabel('')
        self.memdetailsLabel = QtGui.QLabel('')
        layout.addWidget(self.memmeterLabel)
        layout.addWidget(self.memdetailsLabel)

        self.Mem_more.setLayout(layout)

    def  showProcess(self):
        self.Process= QtGui.QGroupBox(" processs status")
        layout = QtGui.QHBoxLayout()
        self.processLabel = QtGui.QLabel('')
        layout.addWidget(self.processLabel)
        self.Process.setLayout(layout)

    def  showbattery(self):
        self.Battery= QtGui.QGroupBox(" battery status")
        layout = QtGui.QHBoxLayout()
        self.batterycapLabel = QtGui.QLabel('')
        self.batterydetailsLabel = QtGui.QLabel('')
        layout.addWidget(self.batterycapLabel)
        layout.addWidget(self.batterydetailsLabel)
        self.Battery.setLayout(layout)

    def  showcsreencap(self):
        self.screencap= QtGui.QGroupBox(" screencap :")
        layout = QtGui.QHBoxLayout()
        self.screencapLabel = QtGui.QLabel('')
        #self.screencapLabel.setMinimumSize(240,250)

        layout.addWidget(self.screencapLabel)
        self.screencap.setLayout(layout)

    def  timeout(self):
        self.memLabel.setPixmap(QPixmap.fromImage(QImage('./out/memsimplearea.png')))
        self.memmeterLabel.setPixmap(QPixmap.fromImage(QImage('./out/memroundmeter.png')))
        
        self.cpuimageLabel.setPixmap(QPixmap.fromImage(QImage('./out/cpumultiline.png')))
        self.processLabel.setPixmap(QPixmap.fromImage(QImage('./out/processmultiline.png')))

        self.batterycapLabel.setPixmap(QPixmap.fromImage(QImage('./out/batterycap.png')))
        self.screencapLabel.setPixmap(QPixmap.fromImage(QImage('./screencap/xmeter.png')).scaled(160,250))

        ###############################
        ## for datails
        lines = os.popen("cat ./out/mem.txt").readlines()
        showlines = ''
        for line in lines:
            showlines = showlines + line
        self.memdetailsLabel.setText(showlines)
        lines = os.popen("cat ./out/battery.txt").readlines()
        showlines = ''
        for line in lines:
            showlines = showlines + line
        self.batterydetailsLabel.setText(showlines)

        lines = os.popen("cat ./out/uptime.txt").readlines()
        print lines
        value = lines[0].split()[0]
        self.uptimeLCDNumber.display(value)

    def about(self):
        QtGui.QMessageBox.about(self, "About",
                "The <b>android tool </b> monitor the android memory(total free cached buffers)  "
                "process  cpu battery online ")
    def help(self):
        QtGui.QMessageBox.about(self, "Help",
                "Anymore questions or detail you can feel free to contact me via liyongming1982@163.com")
    def findbug(self):
        print 'findbug'
        self.form = FindbugForm()
        self.form.setModal(True)
        self.form.show()
        #self.form.exec_()
    def simulateTouch(self):
        print 'simulateTouch'
        self.form = SimulateTouch()
        self.form.setModal(True)
        self.form.show()

    def selectProcess(self):
        print 'SelectProcess'
        self.form = SelectProcessForm()
        self.form.setModal(True)
        self.form.show()
    def emmctest(self):
        print 'emmctest'
        self.form = EMMCForm()
        self.form.setModal(True)
        self.form.show()

    def monkeytest(self):
        print 'monkeytest'
        self.form = MonkeyTestForm()
        self.form.setModal(True)
        self.form.show()
    def charactermap(self):
        print 'character map'
        self.form = CharacterWindow()
        #self.form.setModal(True)
        self.form.show()
    def monitorproperty(self):
        print 'monitor property'
        self.form = PropertyForm()
        self.form.show()

    def bootchart(self):
        print 'make bootchart'
        self.form = BootchartForm()
        self.form.show()
    def kmemleak(self):
        print 'detect kmemleak'
        self.form = KmemleakForm()
        self.form.show()

    def __init__(self, parent=None):
        super(MyForm, self).__init__(parent)

        self.centralWidget = QtGui.QWidget()
        self.setCentralWidget(self.centralWidget)
        ##################################################
        ## for toolbar
        #self.statusBar().showMessage('Ready')
        
        exitAction = QtGui.QAction(QtGui.QIcon('./img/exit.jpeg'), '&Exit', self)        
        exitAction.setShortcut('Ctrl+Q')
        exitAction.setStatusTip('Exit application')
        exitAction.triggered.connect(QtGui.qApp.quit)

        

        menubar = self.menuBar()
        
        fileMenu = menubar.addMenu('&File')
        fileMenu.addAction(exitAction)

        self.helpMenu = self.menuBar().addMenu("&Help")
        self.aboutAct = QtGui.QAction("&About", self, triggered=self.about)
        self.aboutHelp = QtGui.QAction("&get help", self, triggered=self.help)
        self.helpMenu.addAction(self.aboutAct)
        self.helpMenu.addAction(self.aboutHelp)

        ###############################################################################
        ##  for toolbar
        exitAction = QtGui.QAction(QtGui.QIcon('./images/exit.jpeg'), 'Exit', self)
        #exitAction.setShortcut('Ctrl+Q')
        exitAction.triggered.connect(QtGui.qApp.quit)

        findbugAction = QtGui.QAction(QtGui.QIcon('./images/findbug.png'), 'find native bug', self)
        findbugAction.triggered.connect(self.findbug)

        simulatetouchAction = QtGui.QAction(QtGui.QIcon('./images/simulatetouch.png'), 'simulate touch', self)
        simulatetouchAction.triggered.connect(self.simulateTouch)

        emmctestAction = QtGui.QAction(QtGui.QIcon('./images/emmc.png'), 'emmc test', self)
        emmctestAction.triggered.connect(self.emmctest)

        monkeyAction = QtGui.QAction(QtGui.QIcon('./images/monkey.jpg'), 'monkey test', self)
        monkeyAction.triggered.connect(self.monkeytest)
    
        selectprocessAction = QtGui.QAction(QtGui.QIcon('./images/selectprocess.png'), 'select process', self)
        selectprocessAction.triggered.connect(self.selectProcess)

        charactermapAction = QtGui.QAction(QtGui.QIcon('./images/ascii.png'), 'character map', self)
        charactermapAction.triggered.connect(self.charactermap)
      
        propertyAction = QtGui.QAction(QtGui.QIcon('./images/property.png'), 'monitor properties', self)
        propertyAction.triggered.connect(self.monitorproperty)

        bootchartAction = QtGui.QAction(QtGui.QIcon('./images/bootchart.jpg'), 'make bootchart ', self)
        bootchartAction.triggered.connect(self.bootchart)
 
        kmemleakAction = QtGui.QAction(QtGui.QIcon('./images/kmemleak.jpg'), 'detect kernel memory leak', self)
        kmemleakAction.triggered.connect(self.kmemleak)
         

        self.toolbar = self.addToolBar('mytoolbar')

        self.toolbar.addAction(exitAction)
        self.toolbar.addAction(findbugAction)
        self.toolbar.addAction(simulatetouchAction)
        self.toolbar.addAction(emmctestAction)
        self.toolbar.addAction(monkeyAction)
        self.toolbar.addAction(selectprocessAction)
        self.toolbar.addAction(charactermapAction)
        self.toolbar.addAction(propertyAction)
        self.toolbar.addAction(bootchartAction)
        self.toolbar.addAction(kmemleakAction)
        ################################################################################



        
        ##################################################
        #self.createHorizontalGroupBox()

        self.showMem()
        self.showCpu()
        self.showProcess()
        self.showbattery()
        self.showMem_more()
        self.showcsreencap()

        


        #mainLayout = QVBoxLayout()
        mainLayout = QGridLayout()

        self.uptimeLCDNumber = QLCDNumber()
        self.uptimeLCDNumber.setSegmentStyle(QtGui.QLCDNumber.Flat)
        self.uptimeLCDNumber.setNumDigits(6)
    
        
        #mainLayout.addWidget(self.horizontalGroupBox)

        #
        mainLayout.addWidget(self.uptimeLCDNumber,0,1)
        mainLayout.addWidget(self.Mem,1,0)
        mainLayout.addWidget(self.Mem_more,1,1)
 

        mainLayout.addWidget(self.Cpu,2,0)
        mainLayout.addWidget(self.Battery,2,1)
        #mainLayout.addWidget(self.Control)
        mainLayout.addWidget(self.Process,3,0)

        #mainLayout.addWidget(self.Control,3,2)
        mainLayout.addWidget(self.screencap,3,1)

        

        #self.setLayout(mainLayout)
        self.centralWidget.setLayout(mainLayout)
        #androidcontrol.setbrightness(125);

        self.setWindowTitle("X meter")

        self.timer = QTimer()
        self.connect(self.timer, SIGNAL('timeout()'), self.timeout)
        self.timer.start(1000);
        self.timeout()

        self.show()


app = QApplication(sys.argv)
form = None
form = MyForm()
#form.show()
#app.exec_()
sys.exit(app.exec_())




